package com.blog.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.domain.*;
import com.blog.search.SearchService;
import com.blog.search.UpdateIndexService;
import com.google.common.collect.Lists;
import org.apache.lucene.queryParser.ParseException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class BaseController {
	
	@RequestMapping({"/","/index","/index.html"})
	public String index(HttpServletRequest request,HttpServletResponse response){
		List<ArticleMeta> articles = (List<ArticleMeta>)(new ArticleMeta().top(10,null,"order by time desc"));
		request.setAttribute("articles",articles);

		List<Tag> tags = (List<Tag>)(new Tag().top(10,null,"order by time desc"));
		request.setAttribute("tags",tags);

		List<Favorites> fs = (List<Favorites>)(new Favorites().top(5));
		request.setAttribute("fs",fs);

		return "/page/index/index.jsp";
	}
	
	@RequestMapping({"/archive","/archive.html"})
	public String archive( HttpServletRequest request,HttpServletResponse response ){
		ArticleMeta meta = new ArticleMeta();
		List<ArticleMeta> metas = (List<ArticleMeta>)meta.getsByWhere(null);
		Map<String,Map<String,List<ArticleMeta>>> sortedMetas = meta.sortByTime(metas);
		request.setAttribute("metas",sortedMetas);
		return "/page/archive/archive.jsp";
	}
	
	@RequestMapping({"/tag","/tag.html"})
	public String tag( HttpServletRequest request,HttpServletResponse response ){
		Tag tag = new Tag();
		List<Tag> tags = (List<Tag>)tag.getsByWhere(null);

		request.setAttribute("tags",tags);
		return "/page/tag/tag.jsp";
	}
	
	@RequestMapping({"/article/{stitle:[\\w]+}","/article/{stitle:[\\w]+}.html"})
	public String article( HttpServletRequest request,HttpServletResponse response ,@PathVariable String stitle){
		ArticleMeta meta = new ArticleMeta();
		meta = (ArticleMeta)meta.getByWhere(String.format("where stitle='%s'",stitle));
		Article article = new Article();
		article = (Article)article.getByWhere(String.format("where aid=%d",meta.get_id()));

		request.setAttribute("meta",meta);
		request.setAttribute("article",article);
		return "/page/article/article.jsp";
	}

	@RequestMapping({"/favorites","/favorites.html"})
	public String favorites(HttpServletRequest request){
		List<Favorites> fs = Lists.newArrayList();
		User user = WebUtils.getUser(request);
		String sql = null;
		if( user == null ){
			sql = " where ops = 0";
		}
		fs = (List<Favorites>)(new Favorites().getsByWhere(sql));
		request.setAttribute("data",fs);
		return "/page/pub/pub.jsp";
	}

	@RequestMapping({"/search"})
	public String search( HttpServletRequest request) throws IOException, ParseException {
		long begin = System.currentTimeMillis();
		String q = request.getParameter("q");
		if( q == null || q.isEmpty() ){
			return "/favorites";
		}
		List<Favorites> fs = SearchService.search(q,100);
		User user = WebUtils.getUser(request);
		if( user == null ){
			List<Favorites> temp = new ArrayList<Favorites>(fs.size());
			for( Favorites f : fs ){
				if( f.getOps() == 1 ){
					continue;
				}
				temp.add(f);
			}
			fs = temp;
		}
		request.setAttribute("data",fs);
		request.setAttribute("time",System.currentTimeMillis() - begin);
		return "/page/pub/pub.jsp";
	}

	@RequestMapping({"/jump"})
	public String jump(HttpServletRequest request){
		String url = request.getParameter("u");
		if( url == null || url.isEmpty() ){
			return "/";
		}
		request.setAttribute("url",url);
		return "/page/base/jump.jsp";
	}

}
